package com.hnevc.netfoodchoose;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerViewAccessibilityDelegate;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class ShowFoodActivity extends AppCompatActivity {

    List<Food> mFoodList; //食物列表
    //ListView mListView;
    RecyclerView mRecyclerView;
    MyBaseAdapter mAdapter; //适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_food);
        initView();
        initData();
    }

    private void initView() {
        //mListView = (ListView) findViewById(R.id.id_lv_food_list);
        mRecyclerView = (RecyclerView) findViewById(R.id.id_rv_food_list);
        ImageView mIbQuery = (ImageView) findViewById(R.id.id_query);

    }

    /**
     * 初始化数据，用来获取食物列表的数据
     */
    private void initData() {
        //TODO 访问网络获取数据，填充mFoodList,然后创建数据适配器，更新界面
        //访问网络共获取数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                //1 构造json对象，得到json字符串

                //3 创建请求对象
                Request request = new Request.Builder()
                        .url("http://10.1.66.232:8080/RestaurantServer/Food/getFoods")
                        .get()
                        .build();
                //4 创建客户端，发送请求

                OkHttpClient client = new OkHttpClient.Builder()
                        .cookieJar(new MyCookjejar())
                        .build();
                try{
                    Response response = client.newCall(request).execute();
                    String data = response.body().string();
                    //TODO 解析data，将其转换为mFoodList
                } catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //TODO 根据数据mFoodList，自定义数据适配器
    class MyBaseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mFoodList.size();
        }

        @Override
        public Object getItem(int i) {
            return mFoodList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return mFoodList.get(i).getFoodId();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(ShowFoodActivity.this, R.layout.food_list_item, null);

            //获取当前位置对应的数据
            Food food = mFoodList.get(i);
            TextView tvFoodName = (TextView) view. findViewById(R.id.id_tv_foodName);
            tvFoodName.setText(food.getFoodName());
            //自己添加
            //显示图片
            //获取显示图片的软件
            ImageView ivPic = (ImageView) view.findViewById(R.id.id_iv_foodPic);
            String imageurl = "http://10.1.66.232;8080/RestaurantServer/uploadFoodImage/"
                    +food.getFoodPic();
            //利用picasso加载显示图片到ivpic
            Picasso.with(ShowFoodActivity.this)
                    .load(imageurl)
                    .into(ivPic);


            return  view;

        }
    }
}
